/*
 * 题目描述：还是排队问题，但是更烦，加入了VIP插队机制
 * 
 * 坑：每队人最多只能打2小时；当VIP用户到了的时候会优先选择VIP桌序号小的桌子；21点以后店关门
 */
#include <iostream>
#include <vector>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <climits>
using namespace std;

struct Player{
    int arriveTime;
    int total;
    int serveTime;
    int isVip;
    int waittime;
    bool isFinish;
};

struct Window{
    int finishTime;
    int isVip;
    int cnt;
};


vector<Window> windows;
vector<Player> players;
int playerNum, winNum, vipwNum;

Window* getEarliesWin(Player player){
    int minIndex = -1;
    int minValue = INT_MAX;
    for (int i = 0; i < windows.size(); i++) {
        if (player.arriveTime >= windows[i].finishTime) {
            minIndex = i;
            break;
        }
        if (windows[i].finishTime < minValue) {
            minValue = windows[i].finishTime;
            minIndex = i;
        }
    }
    return &windows[minIndex];
}

bool cmpByArrivetime(const Player &p1, const Player &p2){
    return p1.arriveTime <= p2.arriveTime;
}

bool cmpByServetime(const Player &p1, const Player &p2){
    return p1.serveTime <= p2.serveTime;
}

void play(Player *currentPlayer, Window *earliestWin){
    currentPlayer->isFinish = true;
    currentPlayer->serveTime = earliestWin->finishTime < currentPlayer->arriveTime ? currentPlayer->arriveTime
                                                                                   : earliestWin->finishTime;
    currentPlayer->waittime = currentPlayer->serveTime - currentPlayer->arriveTime;
    earliestWin->finishTime = currentPlayer->serveTime + currentPlayer->total * 60;
    if (currentPlayer->serveTime < 21 * 60 * 60) {
        earliestWin->cnt++;
    }
}

int main() {
    cin >> playerNum;
    int h, m, s, t, isVip;
    Player p;
    for (int i = 0; i < playerNum; i++) {
        scanf("%d:%d:%d %d %d", &h, &m, &s, &t, &isVip);
        p.arriveTime = h * 60 * 60 + m * 60 + s;
        p.isVip = isVip;
        //最多只能玩两个小时
        p.total = t <= 120 ? t : 120;
        p.isFinish = false;
        players.push_back(p);
    }
    cin >> winNum >> vipwNum;
    Window w;
    //窗口当前用户结束时间初始化为8点
    w.finishTime = 8 * 60 * 60;
    w.cnt = 0;
    for (int i = 0; i < winNum; i++) {
        windows.push_back(w);
    }
    int num;
    for (int i = 0; i < vipwNum; i++) {
        cin >> num;
        windows[num - 1].isVip = 1;
    }

    stable_sort(players.begin(), players.end(), cmpByArrivetime);
    Player* currentPlayer = NULL;
    Window* earliestWin = NULL;
    for (int i = 0; i < playerNum; i++) {
        currentPlayer = &players[i];
        if (currentPlayer->isFinish) {
            continue;
        }
        //获取最早结束的一个窗口或者序号最小的可用窗口
        earliestWin = getEarliesWin(*currentPlayer);
        //1.最早的窗口是VIP窗口
        if (earliestWin->isVip == 1) {
            //1.1 当前用户是VIP用户
            if (currentPlayer->isVip == 1) {
                //分配窗口
                play(currentPlayer, earliestWin);
            }
            //1.2 当前用户不是VIP用户
            else{
                //判断是否当前队列里是否有VIP用户也在等待
                for (int j = i; j < players.size(); j++) {
                    //如果找到了就将窗口分配给该Vip用户
                    if (earliestWin->finishTime >= players[j].arriveTime
                        && players[j].isVip == 1 &&
                        !players[i].isFinish) {
                        currentPlayer = &players[j];
                        //错误: 起初这里没有减导致部分玩家漏掉了，因为这里被插队了，所以i不能加1
                        i--;
                        break;
                    }
                }
                //如果没有找到就分配给当前用户
                play(currentPlayer, earliestWin);
            }
        }
        //2.最早的窗口不是VIP窗口
        else{
            //2.1 当前用户是VIP用户
            if (currentPlayer->isVip == 1) {
                //判断在该用户到达的时候是否有其他的VIP窗口，如果有则优先选择VIP窗口
                for (int i = 0; i < windows.size(); i++) {
                    if (windows[i].isVip == 1 && windows[i].finishTime <= currentPlayer->arriveTime) {
                        earliestWin = &windows[i];
                        break;
                    }
                }
                //如果没有则分配当前窗口
                play(currentPlayer, earliestWin);
            }
            //2.2 当前用户不是VIP用户
            else{
                //直接分配
                play(currentPlayer, earliestWin);
            }
        }
    }

    stable_sort(players.begin(), players.end(), cmpByServetime);

     for (int i = 0; i < players.size(); i++) {
        //在21点之后的均排除
        if (players[i].serveTime >= 21 * 60 * 60) {
            continue;
        }
        Player p = players[i];
        int arriveH, arriveM, arriveS;
        int serveH, serveM, serveS;
        int waittime;
        arriveH = p.arriveTime / 60 / 60;
        arriveM = (p.arriveTime - arriveH * 60 * 60) / 60;
        arriveS = p.arriveTime - arriveH * 60 * 60 - arriveM * 60;
        serveH = p.serveTime / 60 / 60;
        serveM = (p.serveTime - serveH * 60 * 60) / 60;
        serveS = p.serveTime - serveH * 60 * 60 - serveM * 60;
        waittime = (p.waittime + 30) / 60;
        printf("%02d:%02d:%02d %02d:%02d:%02d %d\n", arriveH, arriveM, arriveS, serveH, serveM, serveS, waittime);
    }
    for (int i = 0; i < windows.size(); i++) {
        cout << windows[i].cnt;
        if (i != windows.size() - 1) {
            cout << " ";
        }
    }
    return 0;
}